package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.service.SysUsrService;
import com.example.entity.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author caozhiren
 * @since 2024-02-23
 */
@Controller
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUsrService sysUserService;

    /**
     * 跳转新增页面
     */
    @RequestMapping(value = "/toInsert")
    public Object toInsert() {
        return "/user/add";
    }

    /**
     * 增
     */
    @RequestMapping(value = "/insert")
    @ResponseBody
    public Object insert(SysUserVo sysUser) {
        return sysUserService.save(sysUser);
    }

    /**
     * 删
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer id) {
        return sysUserService.removeById(id);
    }

    /**
     * 跳转修改页面
     */
//
    @RequestMapping(value = "/toUpdate")
    public Object toUpdate(Integer id, Model model) {
        model.addAttribute("user", sysUserService.getById(id));
        return "/user/edit";
    }

    /**
     * 改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SysUserVo sysUser) {
        return sysUserService.updateById(sysUser);
    }


    /**
     * 分页列表
     */
    @RequestMapping(value = "list")
    public Object getUserPage(Page page, SysUserVo sysUser, Model model) {
        Page pageEntity = sysUserService.pageRecords(page, new QueryWrapper<>(sysUser));
        model.addAttribute("userPager", pageEntity);
        return "user/list";
    }
}
